import  { AppPostT } from '@utils/request'
import { BasicPageSearch, ActionResult } from '@/models/basicType'
import {
    CoursePageSearch, CoursePageResult, CourseJson, CourseProgressReport, CourseProgressQuery, CourseProgressResult, PlayAuthJson,
    RecCoursePageSearch, RecCoursePageResult, HotCourseSearch, HotCourseResult
} from '@/models/course'

// 获取全部课程列表
export async function getCourses(data:CoursePageSearch) {
    const res = await AppPostT<CoursePageResult>("/course/list",data)
    console.log("getCourses request:", data, ", result:", res)
    return res.data ? res.data : {} as CoursePageResult
}

// 获取课程详情（JSON）
export async function getCourseDetail(srcCourseId:string, silent:boolean = false) {
    const data = { srcCourseId }
    const config = { silent }
    const res = await AppPostT<CourseJson>("/course/detail",data,config)
    console.log("getCourseDetail request:", data, ", result:", res)
    return res.data ? res.data : {} as CourseJson
}

// 上报课程进度
export async function reportProgress(data:CourseProgressReport) {    
    const res = await AppPostT<ActionResult>("/course/reportProgress",data,{silent:true})
    console.log("reportProgress request:", data, ", result:", res)
    return res.data ? res.data : {} as ActionResult
}

// 查询课程进度
export async function queryProgress(data:CourseProgressQuery) {    
    const res = await AppPostT<CourseProgressResult>("/course/queryProgress",data)
    console.log("queryProgress request:", data, ", result:", res)
    return res.data ? res.data : 0
}

// 授权播放
export async function getPlayAuth(vid:string) {
    const data = { vid }
    const res = await AppPostT<PlayAuthJson>("/course/playAuth",data)
    console.log("getPlayAuth request:", data, ", result:", res)
    return res.data ? res.data : {} as PlayAuthJson
}

// 加入课程学习
export async function joinCourse(srcCourseId:string) {
    const data = { srcCourseId }
    const res = await AppPostT<ActionResult>("/course/joinCourse",data)
    console.log("joinCourse request:", data, ", result:", res)
    return res.data ? res.data : {} as ActionResult
}

// 获取我的课程列表(分页)
export async function getMyCourses(data:BasicPageSearch) {    
    const res = await AppPostT<RecCoursePageResult>("/course/my/list",data)
    console.log("getMyCourses request:", data, ", result:", res)
    return res.data ? res.data : {} as RecCoursePageResult
}

// 获取推荐课程列表
export async function getRecCourses(data:RecCoursePageSearch) {    
    const res = await AppPostT<RecCoursePageResult>("/course/rec/list",data)
    console.log("getRecCourses request:", data, ", result:", res)
    return res.data ? res.data : {} as RecCoursePageResult
}

// 设置课程最后学习章节（lastSeq：从0开始）
export function setCourseLastSeq(srcCourseId:string, lastSeq: number) {
    localStorage.setItem(generateCourseKey(srcCourseId), lastSeq.toString());
    return true;
}

// 获取课程最后学习章节（结果：从0开始）
export function getCourseLastSeq(srcCourseId:string) {
    const res = localStorage.getItem(generateCourseKey(srcCourseId));
    return res ? parseInt(res) : 0;
}

// 获取热门课程
export async function getHotCourses(data:HotCourseSearch, silent:boolean = false) {    
    const config = { silent }
    const res = await AppPostT<HotCourseResult>("/course/hot/list",data,config)
    console.log("getHotCourses request:", data, ", result:", res, ", config:", config)
    return res.data ? res.data : {} as HotCourseResult
}

// 生成课程本地存储信息key
function generateCourseKey(srcCourseId:string) {
    return "course-" + srcCourseId;
}
